package svc

import (
	"github.com/redis/go-redis/v9"
	"github.com/zeromicro/go-zero/rest"
	"gorm.io/gorm"
	"insighttracking/apps/sorting/internal/config"
	"insighttracking/apps/sorting/internal/middleware"
	"insighttracking/database"
	"strconv"
)

type ServiceContext struct {
	Config           config.Config
	AuthMiddleware   rest.Middleware
	LoggerMiddleWare rest.Middleware

	RedisDb *redis.Client
	MysqlDb *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	redisDb := database.NewRedisDB(c.RedisConfig.Host, c.RedisConfig.Username, c.RedisConfig.Pass, c.RedisConfig.Port, c.RedisConfig.Db)
	mysqlDb := database.NewMysqlDB(c.MysqlConfig.Username, c.MysqlConfig.Password, c.MysqlConfig.Host, strconv.Itoa(c.MysqlConfig.Port), c.MysqlConfig.Database, c.MysqlConfig.Charset, c.MysqlConfig.Loc)
	return &ServiceContext{
		Config:           c,
		AuthMiddleware:   middleware.NewAuthMiddleware(redisDb).Handle,
		LoggerMiddleWare: middleware.NewLoggerMiddleWare().Handle,
		RedisDb:          redisDb,
		MysqlDb:          mysqlDb,
	}
}
